package com.dib.neo.bkfs.server;

import com.dib.neo.bkfs.config.Properties2;
import org.apache.bookkeeper.util.LocalBookKeeper;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class BKFSServerMain {

  public static final String CONF_BKFS_PROPERTIES = "conf/bkfs.properties";

  public static void main(String[] args) throws Exception {
    Thread t = new Thread() {
      @Override
      public void run() {
        ZooKeeperMainWrapper.main();
      }
    };
    t.start();


    Properties properties = new Properties2();

    File configFile = new File(CONF_BKFS_PROPERTIES).getAbsoluteFile();
    System.out.println("Reading configuration from " + configFile);
    if (configFile.isFile()) {
      try (InputStreamReader reader = new InputStreamReader(new FileInputStream(configFile),
          StandardCharsets.UTF_8)) {
        properties.load(reader);
      }
    } else {
      URL url = ZooKeeperMainWrapper.class.getClassLoader().getResource(CONF_BKFS_PROPERTIES);
      System.out.println("Reading configuration from " + url);
      try (InputStreamReader reader = new InputStreamReader(url.openStream(),
          StandardCharsets.UTF_8)) {
        properties.load(reader);
      }
    }
    BKFSConfiguration configuration = new BKFSConfiguration(properties);

    EmbeddedBookie bookie = new EmbeddedBookie(configuration);
    bookie.start();//*/
  }

}
